package kr.ac.kongju.gedl.item.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ItemTreeVO  implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3811928354575111686L;
	private String itemId;
	private String levelId;
	private String level;
	private String parentId;
	private String mainTitle;
	private String existImage;
	private String isLeaf;
	private String isText;
	private String iconClsFolder = "icon-folder";
	private String iconClsFile = "icon-file";
	protected List<ItemTreeVO> children = new ArrayList<ItemTreeVO>();

	public ItemTreeVO() {
		super();
	}

	public ItemTreeVO(String levelId) {
		super();
		this.levelId = levelId;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public String getLevelId() {
		return levelId;
	}

	public void setLevelId(String levelId) {
		this.levelId = levelId;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getMainTitle() {
		return mainTitle;
	}

	public void setMainTitle(String mainTitle) {
		this.mainTitle = mainTitle;
	}

	public String getExistImage() {
		return existImage;
	}

	public void setExistImage(String existImage) {
		this.existImage = existImage;
	}

	public String getIsLeaf() {
		return isLeaf;
	}

	public void setIsLeaf(String isLeaf) {
		this.isLeaf = isLeaf;
	}

	public String getIsText() {
		return isText;
	}

	public void setIsText(String isText) {
		this.isText = isText;
	}

	public String getIconClsFolder() {
		return iconClsFolder;
	}

	public void setIconClsFolder(String iconClsFolder) {
		this.iconClsFolder = iconClsFolder;
	}

	public String getIconClsFile() {
		return iconClsFile;
	}

	public void setIconClsFile(String iconClsFile) {
		this.iconClsFile = iconClsFile;
	}

	public List<ItemTreeVO> getChildren() {
		return children;
	}

	public void setChildren(List<ItemTreeVO> children) {
		this.children = children;
	}
	public ItemTreeVO addChild(ItemTreeVO itemTree){
		ItemTreeVO retTree = itemTree;
		if(retTree != null){
			if(!this.children.add(itemTree)){
				retTree = null;
			}
		}
		return retTree;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((levelId == null) ? 0 : levelId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ItemTreeVO other = (ItemTreeVO) obj;
		if (levelId == null) {
			if (other.levelId != null)
				return false;
		} else if (!levelId.equals(other.levelId))
			return false;
		return true;
	}
	@Override
	public String toString(){
    	StringBuffer buff = new StringBuffer();
    	
    	buff.append("{\"text\": \"" + this.mainTitle + "\",");
		buff.append("\"id\": \"" + this.levelId + "\",");
		buff.append("\"attributes\": {\"textYn\":\"" + this.isText + "\"}");
		
		if(this.isLeaf.equals("1")){
			if(this.existImage.equals("1")){
				buff.append(",\"iconCls\": " + "\"icon-viewImg\"");
			}else{
				buff.append(",\"iconCls\": \""+ this.iconClsFile + "\"");
			}
			buff.append(",\"state\": " + "\"open\"");
		}else{
			if(this.existImage.equals("1")){
				buff.append(",\"iconCls\": " + "\"icon-folder-viewImg\"");
			}else{
				if(this.isText.equals("1")){
					buff.append(",\"iconCls\": " + "\"icon-folder-textImg\"");
				}else{
					buff.append(",\"iconCls\": \"" + this.iconClsFolder + "\"");
				}
			}
			if(this.children.size() > 0){
				buff.append(",\"state\": " + "\"open\"");
				
				buff.append(",\"children\":[");
				int i = 0;
				for(i = 0; i < this.children.size()-1; i++){
					buff.append(this.children.get(i).toString());				
					buff.append(",");
				}
				buff.append(this.children.get(i).toString());
				buff.append("]");
			}else{
				buff.append(",\"state\": " + "\"closed\"");
			}
		}
		
		buff.append("}");

		return buff.toString();
	}
}
